<?php


namespace app\admin\controller;


use app\BaseController;
use think\facade\Filesystem;

class Image extends BaseController
{
    public function upload()
    {
        if (!request()->isPost()) {
            return show(2, '请求不合法');
        }

        $file = request()->file('file');

        $filename = Filesystem::disk('public')->putFile('upload/image', $file);

        if (!$filename) {
            return show(2, 'error');
        }

        return show(1, 'ok', ['image' => '/storage/' . $filename]);
    }

    public function layUpload()
    {
        if (!request()->isPost()) {
            return show(2, '请求不合法');
        }

        $file = request()->file('file');
        $filename = Filesystem::disk('public')->putFile('upload/image', $file);

        if (!$filename) {
            return show(2, 'error');
        }

        return response(json_encode([
            'code' => 0,
            'msg' => '',
            'data' => [
                'src' => '/storage/' . $filename,
                'title' => '',
            ]
        ]),JSON_UNESCAPED_UNICODE);
    }
}